#include "cpar.h"

/**
 * TODO: Implementar como AVL
 **/

typedef struct CPARDictionaryKeyValuePair CPARDictionaryKeyValuePair;

struct CPARDictionaryKeyValuePair
{
  void  *key;
  void  *value;
};

struct CPARDictionary
{
  CPARAllocator               *allocator;
  CPARDictionaryKeyValuePair  *pairs;
  long                        count;
  long                        size;
  CPARDictionaryKeyFn         fnKey;
  long                        allocatorIdx;
};

static 
CPARDictionaryKeyValuePair *
_CPARDictionaryFindPair(CPARDictionary *dic, void *key)
{
  long idx;

  for(idx = 0; idx < dic->count; idx++)
  {
    if(dic->fnKey(key,dic->pairs[idx].key))
      continue;

    return &(dic->pairs[idx]);
  }

  return NULL;
}


cpar_status 
CPARDictionaryCreate( CPARAllocator *allocator, CPARDictionary **dic, CPARDictionaryKeyFn keyFn )
{
  CPARDictionary *p;

  p = (CPARDictionary*) CPARAllocatorAlloc(allocator,sizeof(CPARDictionaryKeyValuePair));
  if(NULL == p)
    return CPAR_E_NO_MEMORY;

  p->allocator  = allocator;
  p->count      = 0;
  p->fnKey      = keyFn;
  p->pairs      = NULL;
  p->size       = 0;
  p->allocatorIdx = CPARAllocatorDestroyListenerAdd(allocator,(CPARAllocatorListener) CPARDictionaryDestroy, p);

  return CPAR_SUCCESS;
}

void 
CPARDictionaryDestroy( CPARDictionary *dic )
{
  if(dic->pairs)
    CPARAllocatorFree(dic->allocator,dic->pairs);

  CPARAllocatorDestroyListenerRemove(dic->allocator, dic->allocatorIdx);

  CPARAllocatorFree(dic->allocator,dic);
}

void 
CPARDictionaryAdd( CPARDictionary *dic, void *key, void *value )
{

}
